<?php

namespace App\Admin\Controllers;

use App\Admin\Del\CouponController;
use App\Admin\Repositories\GrCoupon;
use App\Admin\show\goods;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class GrCouponController extends CouponController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GrCoupon(), function (Grid $grid) {
            $grid->column('coupon_id', 'ID')->sortable();
            $grid->column('coupon_name');
            $grid->column('coupon_state', '状态')->radio(['上架', '下架']);
            $grid->column('coupon_min_price', '触发价格')->display(function ($e) {
                return $e . '元';
            });
            $grid->column('coupon_stock')->display(function ($e) {
                return $e . '张';
            });
            $grid->column('coupon_price')->display(function ($e) {
                return $e . '元';
            })->label();
            $grid->column('coupon_type')->radio(['满减券', '立减券']);
            $grid->column('coupon_distribution_method', '发放方式')->radio([1 => '免费领取', 2 => '积分兑换', 3 => '后台发放']);
            $grid->column('exchange_integral_number', '所需积分')->display(function ($e) {
                return $e . '积分';
            })->label();
            $grid->column('coupon_application', '优惠券范围')->radio([1 => '通用券', 2 => '指定店铺可用', 3 => '指定商品']);
            $grid->column('coupon_astrict_type')->radio(['互斥', '共享']);
            $grid->column('coupon_effective_day')->display(function ($e) {
                return $e . '天';
            })->label();
//            $grid->column('shop_id');
            $grid->column('coupon_goods_id', '指定商品')->display('点击查看')->modal(function ($modal) {
                $modal->title('指定商品');
                return goods::make(['goods_id' => $this->coupon_goods_id]);
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('coupon_name');
                $filter->equal('coupon_state')->select(['已上架', '已下架']);
                $filter->equal('coupon_type')->select(['满减券', '立减券']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GrCoupon(), function (Show $show) {
            $show->field('id');
            $show->field('coupon_name');
            $show->field('coupon_state');
            $show->field('coupon_min_price');
            $show->field('coupon_stock');
            $show->field('coupon_price');
            $show->field('coupon_type');
            $show->field('coupon_application');
            $show->field('coupon_astrict_type');
            $show->field('coupon_effective_day');
            $show->field('shop_id');
            $show->field('coupon_goods_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GrCoupon(), function (Form $form) {
            $form->display('coupon_id');
            $form->text('coupon_name')->required();
            $form->text('coupon_content', '优惠券简介')->required();
            $form->radio('coupon_state', '上下架状态')->options(['上架', '下架'])->default(0);
            $form->text('coupon_min_price');
            $form->radio('coupon_distribution_method', '发放方式')->when(2, function (Form $form) {
                $form->number('exchange_integral_number', '所需积分')->required()->min(1);
            })->options([1 => '免费领取', 2 => '积分兑换', 3 => '后台发放'])->default(1);
            $form->number('coupon_stock')->required();
            $form->text('coupon_price', '优惠价格(元)')->required();
            $form->radio('coupon_type')->required()->options(['满减券', '立减券'])->default(1);
            $form->radio('coupon_astrict_type')->required()->options(['互斥', '共享'])->default(1);
            $form->radio('coupon_application', '适用范围')->when(3, function (Form $form) {
                $form->multipleSelectTable('coupon_goods_id', '请指定商品')//类型为指定商品优惠券就选择商品
                ->from(UserTable::make())->saving(function ($e) {
                    return $e[0];
                });
            })->options([1 => '全平台通用', 2 => '指定店铺可用', 3 => '指定商品类型可用'])->default(1);
            $form->text('coupon_effective_day')->required()->type('number')->maxLength(5)->minLength(1)->default(7);
//            $form->text('shop_id');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
